package com.yaowk.service.mall.clothing.service.impl;

import com.yaowk.api.mall.clothing.model.Address;
import com.yaowk.api.mall.clothing.service.IAddressService;
import com.yaowk.service.common.BaseRepository;
import com.yaowk.service.common.BaseServiceImpl;
import com.yaowk.service.mall.clothing.common.StatusDict;
import com.yaowk.service.mall.clothing.dao.AddressDao;
import com.yaowk.service.mall.clothing.entity.CmAddress;
import com.yaowk.service.mall.clothing.entity.CmMember;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

@Service
public class AddressServiceImpl extends BaseServiceImpl<Address, CmAddress, Integer> implements IAddressService {

    @Autowired
    private AddressDao dao;

    @Override
    @Transactional
    public Address save(Address model) {
        if (model.isNew()) {
            model = simpleSave(model);
        } else {
            CmAddress address = dao.findOne(model.getId());
            if (isNotEmpty(model.getAddress()))
                address.setAddress(model.getAddress());
            if (isNotEmpty(model.getCity()))
                address.setCity(model.getCity());
            if (isNotEmpty(model.getConsignee()))
                address.setConsignee(model.getConsignee());
            if (isNotEmpty(model.getDistrict()))
                address.setDistrict(model.getDistrict());
            if (isNotEmpty(model.getPhone()))
                address.setPhone(model.getPhone());
            if (isNotEmpty(model.getProvince()))
                address.setProvince(model.getProvince());
            if (StatusDict.NORMAL.equals(model.getIsDefault())) {
                // 设为默认，将其他地址清除默认
                CmAddress defaultAddress = dao.findByMemberIdAndIsDefault(address.getMember().getId(), StatusDict.NORMAL);
                if (isNotEmpty(defaultAddress)) {
                    defaultAddress.setIsDefault(StatusDict.DELETE);
                    dao.save(defaultAddress);
                }
                address.setIsDefault(StatusDict.NORMAL);
            }
            model = entity2model(dao.save(address));
        }
        return model;
    }

    @Override
    protected BaseRepository getDao() {
        return dao;
    }

    @Override
    protected CmAddress model2Entity(Address model) {
        CmAddress cmAddress = null;
        if (isNotEmpty(model)) {
            cmAddress = new CmAddress();
            cmAddress.setIsDefault(model.getIsDefault());
            cmAddress.setProvince(model.getProvince());
            cmAddress.setPhone(model.getPhone());
            cmAddress.setDistrict(model.getDistrict());
            cmAddress.setConsignee(model.getConsignee());
            cmAddress.setCity(model.getCity());
            cmAddress.setAddress(model.getAddress());
            if (isNotEmpty(model.getMemberId())) {
                CmMember cmMember = new CmMember();
                cmMember.setId(model.getMemberId());
                cmAddress.setMember(cmMember);
            }
        }
        return cmAddress;
    }

    @Override
    protected Address entity2model(CmAddress model) {
        Address address = null;
        if (isNotEmpty(model)) {
            address = new Address();
            address.setIsDefault(model.getIsDefault());
            address.setProvince(model.getProvince());
            address.setPhone(model.getPhone());
            address.setDistrict(model.getDistrict());
            address.setConsignee(model.getConsignee());
            address.setCity(model.getCity());
            address.setAddress(model.getAddress());
            address.setMemberId(model.getMember().getId());
        }
        return address;
    }
}
